import { createStore, applyMiddleware } from 'redux'
import axios from 'axios'
import logger from 'redux-logger'
import thunk from 'redux-thunk'
const url = "https://api-hmugo-web.itheima.net/api/public/v1/home/swiperdata"


let reducer = (state, action) => {
    // 骗react-redux进行更新
    let NewState = JSON.parse(JSON.stringify(state))
    switch (action.type) {
        case "ADD_NUM":
            NewState.num += 1
            return NewState
        case "INIT_LIST":
            NewState.list = action.list
            return NewState

        
        // case "INIT_LIST":
        //     // reducer函数是一个纯函数(不能写异步操作)  使用到了中间件
        //     axios.get(url).then(res => {
        //         NewState.list = res.data.message
        //     })
        //     return NewState
        default:
            return state
    }
}

let initState = {
    num: 200,
    list: []
}

// createStore(reducer, initState, 应用中间件)
let store = createStore(reducer, initState, applyMiddleware(thunk))

// 1. 使用redux-thunk以后  store.dispatch源代码发生了改变
// console.log(store.dispatch(() => {
//     axios.get(url).then(res => {
//         store.dispatch({ type: "INIT_LIST" })
//     })
// }))

// console.log(store.dispatch({ type: "AA" }))

export default store